import { _decorator, Component, EventTouch, instantiate, JsonAsset, Node, Prefab } from 'cc';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
import { BulletItem } from './BulletItem';
import { SHOPITEM } from '../AshFramework/Datas/Constant';
import { NumberIncrementLabel } from './NumberIncrementLabel';
import { PrefsManager } from '../AshFramework/Managers/PrefsManager';
import Banner from '../Banner';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('Shop')
export class Shop extends Component {
    public static Instance: Shop = null;

    @property(Node)
    AmmoContent: Node = null;

    @property(Node)
    AmmoPanel: Node = null;

    @property(Node)
    WeaponPanel: Node = null;

    @property(Node)
    PropsPanel: Node = null;

    @property(Node)
    NonePanel: Node = null;

    @property(NumberIncrementLabel)
    Gold: NumberIncrementLabel = null;

    private _targetPanel: Node = null;

    protected onLoad(): void {
        Shop.Instance = this;
        this.initAmmo();
    }

    protected start(): void {
        this.showPanel(this.AmmoPanel);
    }

    switchPanel(type: SHOPITEM) {
        let panel: Node = null;
        switch (type) {
            case SHOPITEM.AMMO:
                panel = this.AmmoPanel;
                break;
            case SHOPITEM.WEAPON:
                panel = this.WeaponPanel;
                break;
            case SHOPITEM.PROPS:
                panel = this.PropsPanel;
                break;
            case SHOPITEM.NONE:
                panel = this.NonePanel;
                break;
        }
        if (this._targetPanel === panel) return;
        this.showPanel(panel);
    }

    showPanel(panel: Node) {
        if (this._targetPanel) this._targetPanel.active = false;

        this._targetPanel = panel;
        this._targetPanel.active = true;
    }

    initAmmo() {
        ResourceUtil.LoadJson("AmmoData").then((jsonAsset: JsonAsset) => {
            this.AmmoContent.removeAllChildren();
            const json = jsonAsset.json;
            const names: string[] = Object.keys(json);
            for (let i = 0; i < names.length - 6; i++) {
                const data = json[names[i]];
                BundleManager.LoadPrefab(Bundles.Prefabs, "商店_Ammo").then((prefab: Prefab) => {
                    const node: Node = instantiate(prefab);
                    node.getComponent(BulletItem).init(names[i], data);
                    node.parent = this.AmmoContent;
                })
            }
            this.initGrenade();
        })
    }

    initGrenade() {
        BundleManager.LoadPrefab(Bundles.Prefabs, "商店_破片手雷").then((prefab: Prefab) => {
            const node: Node = instantiate(prefab);
            node.getComponent(BulletItem).initGrenade("破片手雷");
            node.parent = this.AmmoContent;
        })
        BundleManager.LoadPrefab(Bundles.Prefabs, "商店_高爆手雷").then((prefab: Prefab) => {
            const node: Node = instantiate(prefab);
            node.getComponent(BulletItem).initGrenade("高爆手雷");
            node.parent = this.AmmoContent;
        })
    }

    showGold(changeGold: number = 0) {
        PrefsManager.Instance.userData.Gold -= changeGold;
        this.Gold.playNumberIncrementTo(PrefsManager.Instance.userData.Gold);
    }

    playViedo() {
        AudioManager.PlaySound(Audios.ButtonClick);
        Banner.Instance.ShowVideoAd(() => {
            this.showGold(-1000);
        })
    }

    protected onEnable(): void {
        this.showGold();
    }

}


